<template>
    <div class="list-page">
        <div class="list-page-header list-page-header-flex">
            <div class="flex-left">
                <el-button type="primary" @click="add">新增</el-button>
            </div>
            <div class="flex-right">
                <el-input v-model="query.projectName" class="search-input-class" placeholder="项目名称" />
                <el-input v-model="query.clientCompanyName" class="search-input-class" placeholder="业主单位名称" />
                <el-button type="primary" @click="loadData">查询</el-button>
                <el-button @click="reset">重置</el-button>
            </div>
        </div>
        <div class="list-content">
            <el-table
                ref="datagrid"
                :data="table.data"
                border
                @row-click="handleClickTableRow"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" />
                <el-table-column prop="projectName" label="项目名" min-width="200" show-overflow-tooltip />
                <el-table-column prop="businessType" label="业务类型" min-width="140" show-overflow-tooltip />
                <el-table-column prop="projectAddrName" label="项目地址" min-width="140" show-overflow-tooltip />
                <el-table-column prop="clientCompanyName" label="业主单位名称" min-width="180" show-overflow-tooltip />
                <el-table-column label="合同额" min-width="120">
                    <template #default="scope">
                        {{ scope.row.amountOfContract / 100 }}
                    </template>
                </el-table-column>
                <el-table-column prop="bidChance" label="中标概率" :formatter="bidFmt" min-width="120" />
                <el-table-column fixed="right" label="操作" width="200">
                    <template #default="scope">
                        <el-button size="small" type="primary" @click="edit(scope.row)">编辑</el-button>
                        <el-popconfirm
                            title="正在进行敏感操作，是否继续？"
                            confirmButtonText="继续"
                            cancelButtonText="取消"
                            @confirm="del(scope.row)"
                        >
                            <template #reference>
                                <el-button type="danger" size="small">删除</el-button>
                            </template>
                        </el-popconfirm>
                        <el-button size="small" type="warning" @click="track(scope.row)">跟踪</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="list-footer">
            <el-pagination
                background
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="table.pagination.currentPage"
                :page-sizes="table.pagination.pageSizes"
                :page-size="table.pagination.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="table.pagination.total"
            ></el-pagination>
        </div>
        <transition name="slide-fade">
            <div v-if="showInfoWindow" class="list-form-modal">
                <ProjectTrackForm :id="selectedId" :operate="operate" @close="closeInfoWindow"></ProjectTrackForm>
            </div>
        </transition>
    </div>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElNotification } from 'element-plus'
import ProjectTrackForm from './form/ProjectTrackForm.vue'
import http from '@/http.js'

const datagrid = ref(null)

const query = reactive({
    projectName: '',
    clientCompanyName: ''
})

const table = reactive({
    data: [],
    selected: [],
    pagination: {
        currentPage: 1,
        pageSizes: [10, 20, 50, 100],
        pageSize: 10,
        total: 0
    }
})

const selectedId = ref('')

const operate = ref('form')

const showInfoWindow = ref(false)

const handleSelectionChange = (val) => {
    table.selected = val
}

const handleSizeChange = (val) => {
    table.pagination.pageSize = val
    loadData()
}

const handleCurrentChange = (val) => {
    table.pagination.currentPage = val
    loadData()
}

const handleClickTableRow = (row) => {
    datagrid.value.clearSelection()
    datagrid.value.toggleRowSelection(row)
}

const bidFmt = (row, column, cellValue, index) => {
    let data = row[column.property]
    if (data === 'HIGH') {
        return '高'
    } else if (data === 'MIDDLE') {
        return '中'
    } else {
        return '低'
    }
}

const loadData = () => {
    let roles = localStorage.getItem('roles')
    let showAll = false
    if (roles.indexOf('ROLE_FGFZR') > -1 || roles.indexOf('ROLE_ZXLD') > -1 || roles.indexOf('ADMIN') > -1) {
        showAll = true
    }
    let params = {
        projectName: query.projectName,
        clientCompanyName: query.clientCompanyName,
        showAll: showAll,
        page: table.pagination.currentPage,
        pageSize: table.pagination.pageSize
    }
    http.fetchForm({
        url: '/jingying/project/track/grid',
        data: params
    }).then((res) => {
        table.data = res.data.grid
        table.pagination.total = res.data.total
    })
}

const reset = () => {
    query.projectName = ''
    query.clientCompanyName = ''
    loadData()
}

const add = () => {
    operate.value = 'form'
    selectedId.value = ''
    showInfoWindow.value = true
}

const edit = (row) => {
    operate.value = 'form'
    selectedId.value = row.id
    showInfoWindow.value = true
}

const track = (row) => {
    operate.value = 'track'
    selectedId.value = row.id
    showInfoWindow.value = true
}

const del = () => {
    let selected = table.selected
    if (selected.length == 0) {
        ElNotification({
            title: '提示',
            message: '请选择要删除的人员',
            type: 'info'
        })
        return
    }
    http.fetchForm({
        url: '/jingying/project/track/delete',
        method: 'post',
        data: {
            id: selected[0].id
        }
    }).then((res) => {
        ElNotification({
            title: '提示',
            message: '删除成功',
            type: 'success'
        })
        loadData()
    })
}

const closeInfoWindow = () => {
    showInfoWindow.value = false
    loadData()
}

// mounted
onMounted(() => {
    loadData()
})
</script>
<style scoped>
@import '@/assets/css/list-page.css';
</style>
